package com.s10.ecloud.web.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.s10.ecloud.web.dao.ICommandDAO;
import com.s10.ecloud.web.dao.IDeviceDAO;
import com.s10.ecloud.web.dao.IDeviceGroupDAO;
import com.s10.ecloud.web.dao.IDeviceModelDAO;
import com.s10.ecloud.web.dao.IDeviceTypeDAO;
import com.s10.ecloud.web.model.Device;
import com.s10.ecloud.web.model.DeviceGroup;
import com.s10.ecloud.web.model.DeviceModel;
import com.s10.ecloud.web.model.DeviceType;
import com.s10.ecloud.web.model.PageModel;
import com.s10.ecloud.web.service.IDeviceService;

@Service
public class DeviceServiceImpl implements IDeviceService {
	@Resource
	private IDeviceTypeDAO deviceTypeDAO;

	@Resource
	private IDeviceDAO driverDAO;

	@Resource
	private IDeviceModelDAO driverModelDAO;

	@Resource
	private IDeviceGroupDAO driverGroupDAO;

	@Resource
	private ICommandDAO commandDAO;

	@Override
	public void getDevicePageModel(PageModel pageModel) {
		driverDAO.makePageModel(pageModel);
	}

	@Override
	public void getDeviceModelPageModel(PageModel pageModel, int userID) {
		driverModelDAO.makePageModel(pageModel, userID);
	}

	@Override
	public void deleteDevice(PageModel pageModel, int userID, Device device) {
		// TODO Auto-generated method stub
		driverDAO.makePageModel(pageModel);
	}

	@Override
	public void updateDevice(PageModel pageModel, int userID, Device device) {
		// TODO Auto-generated method stub
		driverDAO.makePageModel(pageModel);
	}

	@Override
	public String deleteDeviceModel(PageModel pageModel, int userID, int deviceModeId) {
		DeviceModel model = driverModelDAO.getDeviceModelById(deviceModeId);

		if (model != null) {
			driverModelDAO.deleteDeviceModel(model);
		} else {
			return "删除对象不存在";
		}
		driverModelDAO.makePageModel(pageModel, userID);

		return null;
	}

	@Override
	public String updateDeviceModel(DeviceModel deviceModel) {

		DeviceModel model = driverModelDAO.getDeviceModelById(deviceModel.getId());

		if (model != null) {
			model.setAddress(deviceModel.getAddress());
			model.setDescription(deviceModel.getDescription());
			model.setDeviceType(deviceModel.getDeviceType());
			model.setName(deviceModel.getName());

			driverModelDAO.updateDeviceModel(model);
		} else {
			return "更新对象不存在";
		}
		return null;
	}

	@Override
	public void addDeviceModel(DeviceModel deviceModel) {
		driverModelDAO.addDeviceModel(deviceModel);
	}

	@Override
	public List<DeviceType> getDeviceType() {
		// TODO Auto-generated method stub
		return deviceTypeDAO.getDeviceTypeList();
	}

	@Override
	public void getDeviceGroupPageModel(PageModel pageModel, int userID) {
		driverGroupDAO.makeDeviceGroupPageModel(pageModel, userID);
	}

	@Override
	public String deleteDeviceGroup(PageModel pageModel, int userID, int deviceGroupId) {
		DeviceGroup model = driverGroupDAO.getDeviceGroupById(deviceGroupId);

		if (model != null) {
			driverGroupDAO.deleteDeviceGroup(model);
		} else {
			return "删除对象不存在";
		}
		driverGroupDAO.makeDeviceGroupPageModel(pageModel, userID);

		return null;
	}

	@Override
	public DeviceModel getDeviceModelByID(int id) {
		// TODO Auto-generated method stub
		return driverModelDAO.getDeviceModelById(id);
	}

	@Override
	public DeviceGroup getDeviceGroupByID(int id) {
		return driverGroupDAO.getDeviceGroupById(id);
	}

	@Override
	public String updateDeviceGroup(DeviceGroup deviceGroup) {

		DeviceGroup model = driverGroupDAO.getDeviceGroupById(deviceGroup.getId());

		if (model != null) {
			model.setName(deviceGroup.getName());
			driverGroupDAO.updateDeviceGroup(model);
		} else {
			return "更新对象不存在";
		}
		return null;
	}

	public void getCommandPageModel(PageModel pageModel, int deviceModelId) {
		commandDAO.makePageModel(pageModel, deviceModelId);
	}
}
